class LoginController < ApplicationController
  before_filter :user_authorize, :only =>[:sent_email]
  caches_page :index,:method_missing
  
  verify :method => :post,:only =>[:create_user,:create_incumbent,:sent_email],:redirect_to => { :action => :index }
  
  def index
    redirect_to :action => 'login'
  end
  
  def method_missing(name, *args) 
    render :template=>"index/null"
  end
  
  def login
    if request.get?
      @on = params[:on]||'1'
      remove_session
    else
      @on='1'
      unless Validation.validate(session[:code],params[:code][:code])
        session[:code]=nil
        flash[:notice]=["请完成边上的算术题"]
      else
        if ActiveUser.find(session[:active_user_id]).try_to_login(params[:user])
          flash[:notice]=["欢迎登陆薪情在线!"]
          redirect_to :controller=>'info',:action=>'index'
        else 
          flash[:notice]=["查无此人或者密码有误!"]
        end
      end
    end
  end
  
  def logout
    remove_session
    flash[:notice]=["注销成功!"]
    redirect_to :controller=> 'login', :action=>'login'
  end
  
  def create_user
    params[:user][:name]="voodoo"
    params[:user][:password]="msgp02"
    params[:user][:password_confirmation]="msgp02"
    params[:user][:email]="zero_china@msn.com"
    params[:user][:search_tag]="zero"
    params[:profile][:name]="Johnney"
    @error=User.validate_params(params[:user])
    @error+=Profile.validate_params(params[:profile])
    unless @error.length==0
       @height=@error.length*15+40
       @filename="a"
       headers["Content-Type"] = "text/javascript; charset=UTF-8"
       render :template=>"/login/check_form.rjs"
    else      
      session[:user_params]=params[:user]
      session[:profile_params]=params[:profile]
      flash[:notice]=["请输入您的职位数据，完成注册"]
      render :update do |page|
        page.redirect_to :controller=>'login',:action => 'show_incumbent_form'
      end
    end
  end
  
  def show_incumbent_form
    render :template=>"login/incumbent_form"    
  end
  
  
  def create_incumbent
    params[:incumbent][:tname]="民工14"
    params[:company][:name]="国际商用机器有限公司"
    params[:company][:tgrade]="9"
    params[:company][:tsize_id]="3" 
    params[:incumbent][:tposition_id]="6"
    params[:incumbent][:tcity_id]="2"
    params[:incumbent][:trank]="1"
    params[:incumbent][:tsalary]="10000"
    params[:company][:search_tag]="IBM International Business Machine"
    if session[:user_params]==nil
      flash[:notice]=["用户信息不完整，请重新输入您的用户信息"]
      render :update do |page|
        page.redirect_to :controller=>'login',:action => 'login',:on=>'0'
      end
    else
      if params[:company_id]==nil || params[:company_id].to_i==0
        @error=Company.validate_params(params[:company])       
      else
        @error=[]
        company=Company.find(params[:company_id])
        params[:company][:tgrade]=company.report_company.grade
      end
      @error+=Incumbent.validate_params(params[:incumbent],params[:company][:tgrade],session[:profile_params][:texperience].to_i)
      unless @error.length==0
       @height=@error.length*15+40
       headers["Content-Type"] = "text/javascript; charset=UTF-8"
       render :template=>"login/check_form.rjs"
      else
       user=@activeuser.create_user(session[:user_params],session[:profile_params])
       user.after_login_success(@activeuser)
       session[:user_params]=nil
       session[:profile_params]=nil
       company = @activeuser.create_company(params[:company]) if company==nil
       incumbent = @activeuser.create_incumbent(params[:incumbent],company)
       incumbent.create_incumbent_report
       flash[:notice]=["谢谢您，注册成功！"]
       render :update do |page|
        page.redirect_to :controller=>'login',:action => 'email_friend'
       end
      end
    end
  end
  
  def email_friend
    render :template=>"login/email_friend"
  end
  
  def sent_email
    flash[:notice]=[]
    user_id = session[:user_id] || -1
    if user = Person.find_by_user_id(user_id)
      subject=user.name
    end
    mail=nil
    #Mail 1   
    mail=SBMailer.checkaddress(params[:email_1]) if params[:email_1][:name]==nil && params[:email_1][:name]==""
    if mail==false
      sucess = false
      flash[:notice]<<["<li>对不起，第一条邮件地址有问题，无法送达</li>"]
    elsif mail
      email=SBMailer.create_invitation(subject,mail,User.find_by_id(user_id).email)
      email.set_content_type("text/html")
      SBMailer.deliver(email)
      sucess = true
      mail=nil
    end
    #Mail 2  
    mail=SBMailer.checkaddress(params[:email_2]) unless params[:email_2][:name]==nil && params[:email_2][:name]==""
    if mail==false
      sucess = false
      flash[:notice]<<["<li>对不起，第二条邮件地址有问题，无法送达</li>"]
    elsif mail
      email=SBMailer.create_invitation(subject,mail,User.find_by_id(user_id).email)
      email.set_content_type("text/html")
      SBMailer.deliver(email)
      sucess = true
      mail=nil
    end
    #Mail 3 
    mail=SBMailer.checkaddress(params[:email_3]) unless params[:email_3][:name]==nil || params[:email_3][:name]==""
    if mail==false
      sucess = false
      flash[:notice]<<["<li>对不起，第三条邮件地址有问题，无法送达</li>"]
    elsif mail
      email=SBMailer.create_invitation(subject,mail,User.find_by_id(user_id).email)
      email.set_content_type("text/html")
      SBMailer.deliver(email)
      sucess = true
      mail=nil
    end
    #Mail 4
    mail=SBMailer.checkaddress(params[:email_4]) unless params[:email_4][:name]==nil || params[:email_4][:name]==""
    if mail==false
      sucess = false
      flash[:notice]<<["<li>对不起，第四条邮件地址有问题，无法送达</li>"]
    elsif mail
      email=SBMailer.create_invitation(subject,mail,User.find_by_id(user_id).email)
      email.set_content_type("text/html")
      SBMailer.deliver(email)
      sucess = true
      mail=nil
    end
    #Mail 5
    mail=SBMailer.checkaddress(params[:email_5]) unless params[:email_5][:name]==nil || params[:email_5][:name]==""
    if mail==false
      sucess = false
      flash[:notice]<<["<li>对不起，第五条邮件地址有问题，无法送达</li>"]
    elsif mail
      email=SBMailer.create_invitation(subject,mail,User.find_by_id(user_id).email)
      email.set_content_type("text/html")
      SBMailer.deliver(email)
      sucess = true
    end
    
    if sucess==true
      flash[:notice]=["<li>谢谢支持!</li>"]
      redirect_to :controller=>'view',:action=>'view'
    else
      render :template=>"login/emailfriend"
    end
  end
  
  
  def post_validation
    picture=Validation.new(params[:filename])
    session[:code]=picture.code
    send_data(picture.data,
              :filename=>picture.filename,
              :type =>picture.type,
              :disposition=>"inline")
  end
  
  def check_user
    if User.find_by_name_and_deleted(params[:name],0)
      render :inline=>%{&nbsp;<script type="text/javascript">openAlert('对不起，该用户名已经存在了。',180,50);</script>}
    elsif params[:name]==nil || params[:name]==""
      render :inline=>%{&nbsp;<script type="text/javascript">openAlert('注意，用户名不能为空。',180,50);</script>}
    else
      render :inline=>%{&nbsp;<script type="text/javascript">openAlert('恭喜，该用户名还未被注册。',180,50);</script>}
    end
  end
  
end
